<?php

/**
 * This is the model class for table "{{Role}}".
 *
 * The followings are the available columns in table '{{Role}}':
 * @property integer $role_id
 * @property string $role_name
 * @property string $role_label
 * @property string $acl_desc
 * @property integer $p_id
 * @property string $create_time
 * @property string $update_time
 * @property integer $status
 */
class Role extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Role the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{Role}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('p_id, status', 'numerical', 'integerOnly'=>true),
			array('role_name, role_label', 'length', 'max'=>20),
			array('acl_desc, create_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('role_id, role_name, role_label, acl_desc, p_id, create_time, update_time, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'role_id' => 'Role',
			'role_name' => 'Role Name',
			'role_label' => 'Role Label',
			'acl_desc' => 'Acl Desc',
			'p_id' => 'P',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('role_id',$this->role_id);
		$criteria->compare('role_name',$this->role_name,true);
		$criteria->compare('role_label',$this->role_label,true);
		$criteria->compare('acl_desc',$this->acl_desc,true);
		$criteria->compare('p_id',$this->p_id);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        	
	public function getOneAclArray($roleId=null, $filter= array() )
	{
		if ($roleId==null) {
			throw new CHttpException(404,'RoleId can not be empty!');
			
		} else {
			$filter= array_merge($filter, array('role_id'=> $roleId));
			$result= $this->findByAttributes($filter);
			if($result->getAttribute('acl_desc')=== Yii::app()->params['fullAccess'] ) {
				return Yii::app()->params['fullAccess'];
			} else {
				return unserialize( $result->getAttribute('acl_desc'));
			}
		}
	}
}